
package com.mycompany.mavenproject3;

import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Named;
import javax.persistence.TypedQuery;

@Named("loginController")
@SessionScoped
public class LoginController implements Serializable {
    private Login current;
    @EJB
    private LoginFacade ejbFacade;
    private String message;
     private Login selected;
    
    public LoginController() {
    }
    public Login getSelected() {
        if (current == null) {
            current = new Login();
        }
        return current;
    }
    
    public void setSelected(Login selected) {
        this.selected = selected;
    }

    public LoginFacade getFacade() {
        return ejbFacade;
    }
    public void validateLogin() {
        TypedQuery<Login> query = getFacade().getEntityManager().
                createNamedQuery("Login.findByUsernameAndPassword", Login.class);
        query.setParameter("username", current.getUsername());
        query.setParameter("password", current.getPassword());
        try {
            Login login = query.getSingleResult();
            message = "Valid User";
        } catch (Exception e) {
            message = "Invalid Login Credentials";
        }
    }
    public Login getLogin(java.lang.Integer id) {
        return getFacade().find(id);
    }
    
    public String getMessage() {
        return message;
    }
    
    @FacesConverter(forClass = Login.class)
    public static class LoginControllerConverter implements Converter {

        @Override
        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            LoginController controller = (LoginController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "loginController");
            return controller.getLogin(getKey(value));
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuilder sb = new StringBuilder();
            sb.append(value);
            return sb.toString();
        }

        @Override
        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Login) {
                Login o = (Login) object;
                return getStringKey(o.getUserid());
            } else {
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "object {0} is of type {1}; expected type: {2}", new Object[]{object, object.getClass().getName(), Login.class.getName()});
                return null;
            }
        }

    }

}